module.exports = app => {
  const express = require('express')
  const Teacher = require('../../models/Teacher')
  const Student = require('../../models/Student')
  /***********************测试用接口**************************************/
  const testRouter = express.Router({
    mergeParams: true
  })

  testRouter.get('/', async (req, res) => {
    let limit = parseInt(req.query.size) || 10; //分页参数
    let currentPage = parseInt(req.query.page) || 1; //当前页码
    let params = req.query;
    const queryOptions = {} //要关联其他表的时候的配置
    if (req.Model.modelName === "Teacher") {
      queryOptions.populate = 'department'
    }
    if (req.Model.modelName === "Class") {
      queryOptions.populate = 'superior'
    }
    if (req.Model.modelName == "Student") {
      queryOptions.populate = [{
        path: "class",
        select: ["_id", "name"]
      }, {
        path: "specialty",
        select: ["_id", "name"]
      }]
    }
    if (currentPage < 1) {
      currentPage = 1;
    }
    if (limit < 1) {
      limit = 1;
    }
    queryOptions.limit = limit;
    queryOptions.skip = (currentPage - 1) * limit;
    let vagueMp = []
    for (let i in params) {
      let mp = {}
      if (params[i] !== undefined && i != 'size' && i != 'page' && params[i] && params[i] != "''") {
        let reg = new RegExp(params[i], 'i');
        console.log(reg)
        mp[i] = {
          $regex: reg
        }
        vagueMp.push(mp)
        // mp[i] = params[i]
      }
    }
    console.log(vagueMp)
    // console.log(...mp)
    // return res.json({
    //   success: true,
    //   message: "请求成功",
    //   counts: 10,
    //   list: vagueMp
    // })
    // console.log([...mp])
    req.Model.find({
      $or: vagueMp
    }, (err, ress) => {
      if (err) return res.json({
        success: false,
        message: err
      });
      let all = ress.length;
      req.Model.find({
        // ...mp
        $or: vagueMp
      }).setOptions(queryOptions).exec(async (err, doc) => {
        if (err) {
          return res.json({
            success: false,
            message: err
          });
        }
        if (req.Model.modelName == 'User') {
          let returnDoc = []
          for (let i = 0; i < doc.length; i++) {
            let docs = ''
            if (doc[i].identity == 0) {
              docs = await Student.findById(doc[i].bindId)
            } else if (doc[i].identity == 1 || doc[i].identity == 2) {
              docs = await Teacher.findById(doc[i].bindId);
            }
            let temp = {
              name: docs.name,
              username: doc[i].username,
              username: doc[i].username,
              bindId: doc[i].bindId,
              password: doc[i].password,
              identity: doc[i].identity,
              _id: doc[i]._id
            }
            returnDoc.push(temp)
          }
          return res.json({
            success: true,
            message: "请求成功",
            counts: all,
            list: returnDoc
          })
        } else {
          return res.json({
            success: true,
            message: "请求成功",
            counts: all,
            list: doc
          })
        }
      })
    })
  })

  app.use('/admin/api/test/:resource', async (req, res, next) => {
    try {
      req.Model = require(`../../models/${req.params.resource}`)
      next()
    } catch (error) {
      res.send({
        success: false,
        message: error
      })
    }
  }, testRouter)
}